//
//  CMDBModifyViewController.swift
//  CMDB
//
//  Created by 王磊 on 18/05/2017.
//  Copyright © 2017 CMDB. All rights reserved.
//

import UIKit

class CMDBModifyPhoneViewController: NormalTitleViewController {
    
    internal var phone: CMDBSubRegisterTF = CMDBSubRegisterTF()
    
    internal var verify: CMDBRegisterVcodeTF = CMDBRegisterVcodeTF()
    
    override func setup() {
        super.setup()
        
        buildSubview()
    }
    private var confirmBtn: UIButton = UIButton(type: .Custom)
    
    private func buildSubview() {
        
        contentView?.backgroundColor = UIColor.whiteColor()
        
        contentView!.addSubview(phone)
        
        contentView!.addSubview(verify)
        
        contentView!.addSubview(confirmBtn)
        
        let Left_Margin: CGFloat = (CMDB_Screen_Width - 630 * CMDB_Rate) * 0.5
        
        phone.frame = CGRect(x: Left_Margin, y: 20 , width:630 * CMDB_Rate , height: 60)
        
        let textAttr = [NSFontAttributeName: CMDB_Register_TF_FONT,NSForegroundColorAttributeName: word87Color]
        
        let placeHolderAttr = [NSFontAttributeName: CMDB_Register_TF_FONT,NSForegroundColorAttributeName: CMDB_Register_TF_PlaceHolder_Color]
        
        phone.attrText = NSMutableAttributedString(string: "手机号码:", attributes: textAttr)
        
        phone.placeHolderAttr = NSAttributedString(string: "+86", attributes: placeHolderAttr)
        
        phone.keyboardType = .NumberPad
        
        phone.returnKeyType = .Done
        
        phone.maxLength = 11
        
        verify.attrText = NSMutableAttributedString(string: "验证码:", attributes: textAttr)
        
        verify.placeHolderAttr = NSAttributedString(string: "请输入验证码", attributes: placeHolderAttr)
        
        verify.keyboardType = .NumberPad
        
        verify.returnKeyType = .Done
        
        verify.maxLength = VCode_Length
        
        verify.frame = CGRect(x: Left_Margin, y: 80 , width:630 * CMDB_Rate , height: 60)
        
        verify.addTarget(self, action: #selector(verifyItem), controlEvent: .ItemClick)
        
        confirmBtn.setTitle("确定", forState: .Normal)
        
        confirmBtn.setTitle("确定", forState: .Highlighted)
        
        confirmBtn.setTitleColor(UIColor.Hex6(0xffffff, alpha: 0.54), forState: .Highlighted)
        
        confirmBtn.setTitleColor(UIColor.Hex6(0xffffff), forState: .Normal)
        
        confirmBtn.backgroundColor = UIColor.Hex6(0xff6d00, alpha: 54)
        
        confirmBtn.titleLabel!.font = CMDB_17_FONT
        
        confirmBtn.addTarget(self, action: #selector(confirmBtnTriggrred), forControlEvents: .TouchUpInside)
        
        confirmBtn.layer.cornerRadius = 5
        
        confirmBtn.frame = CGRectMake(Left_Margin, verify.y + 60 + 40, phone.width, 88 * CMDB_Rate)
    }
}

extension CMDBModifyPhoneViewController {
    
    @objc private func verifyItem() {
        
        let vcodeParam = CMDBVCodeParam()
        
        vcodeParam.phone = phone.text
        
        vcodeParam.vcodeType = .BindPhone
        
        do {
            
            try CMDBVCodeManager.getInstance.queryVCode(vcodeParam, handler: { (isVCodePost, vCodeType) in
                
                
                
                }, error: { (rescode) in
                    
            }) {
                
                
            }
            
        } catch CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.PhoneInvalid) {
            
            CMDBShowInfoManager.getInstance.showInfoWithStatus("无效手机号!")
            
        } catch CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.VCodeTypeUnSelected) {
            
            
        } catch {
            
            
        }
    }
    
    @objc private func confirmBtnTriggrred() {
        
        let bindPhone = CMDBBindPhoneParam()
        
        bindPhone.phone = phone.text
        
        bindPhone.vcode = verify.text
        
        do {
            
            try CMDBUserInfoManager.getInstance.bindPhone(bindPhone, handler: { [weak self] (isSuccess, type) in
                
                for item in self!.navigationController!.viewControllers {
                    
                    if item is CMDBSettingViewController {
                        
                        self!.navigationController?.popToViewController(item, animated: true)
                        
                        break
                    }
                }
                
                CMDBShowInfoManager.getInstance.showInfoWithStatus("变更手机号成功!")
                
                }, error: { (rescode) in
                    
                }, failure: {
                    
            })
        } catch CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.PhoneInvalid) {
            
            CMDBShowInfoManager.getInstance.showInfoWithStatus("无效手机号!")
            
        } catch CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.VCodeInvalid) {
            
            CMDBShowInfoManager.getInstance.showInfoWithStatus("请输入验证码")
            
        } catch {
            
            
            
        }
        
    }
}